package com.huayue.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.HousekeepOrder;
import com.huayue.entity.HousekeepOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.HousekeepOrderService;
import com.huayue.service.HousekeepService;
import com.huayue.service.RoomService;
import com.huayue.util.DateHelper;
import com.huayue.util.OrderNumberHelper;
import com.huayue.util.PageHelper;

/**
 * 房务订单
 */
@Controller
@RequestMapping("/housekeepOrder")
public class HousekeepOrderController {
    @Autowired
    HousekeepOrderService svc;
    @Autowired
    RoomService rsvc;
    @Autowired
    HousekeepService hsvc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(HousekeepOrder housekeepOrder) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据前台判断是否需要生成订单号
        if ("true".equals(housekeepOrder.getOrdernumber())) {
            view.addStaticAttribute("orderNumber", OrderNumberHelper.getOrderNumber(OrderNumberHelper.FWORDER, "FW"));
        }
        view.addStaticAttribute("roomInfo", rsvc.findRoom());
        view.addStaticAttribute("housekeepInfo", hsvc.findAll());

        return new ModelAndView(view);
    }

    @RequestMapping("/close")
    public ModelAndView close(HousekeepOrder housekeepOrder) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.FWORDER, housekeepOrder.getOrdernumber());
        view.addStaticAttribute("code", 0);
        return new ModelAndView(view);
    }
    // @RequestMapping("/findDetails")
    // public ModelAndView findDetails(String id) {
    // FastJsonJsonView view = new FastJsonJsonView();
    // view.addStaticAttribute("code", 0);
    // HousekeepOrder order = new HousekeepOrder();
    // saleOrder.setId(Long.valueOf(id));
    // view.addStaticAttribute("order", svc.findOne(order));
    // //view.addStaticAttribute("data", svc.findDetails(id));
    // return new ModelAndView(view);
    // }

    @LogAnno(operateType = "添加-房务订单")
    @RequestMapping("/add")
    public ModelAndView add(@RequestBody String json, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");

        JSONObject jsonData = JSONObject.parseObject(json);

        HousekeepOrder order = new HousekeepOrder();
        order.setOrdernumber(String.valueOf(jsonData.get("orderNumber")));
        order.setEnployee_id_apply(user.getId());
        order.setWorkdate_begin(new Date());
        JSONArray jsonArray = jsonData.getJSONArray("table");

        List<HousekeepOrderDetails> details = JSONArray.parseArray(jsonArray.toString(), HousekeepOrderDetails.class);

        boolean result = svc.addDetails(order, details);
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.FWORDER, order.getOrdernumber());
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        if (result) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "订单添加成功！");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "订单添加失败！");
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "工作-房务订单")
    @RequestMapping("/check")
    public ModelAndView check(HousekeepOrder housekeepOrder, Integer number, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        housekeepOrder.setEnployee_id_ratify(user.getId());
        housekeepOrder.setWorkdate_end(new Date());
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);

        // 查询当前布草总套数
        int overNumber = svc.minDepotNumber();
        if (number > overNumber) {
            view.addStaticAttribute("msg", "布草数量不足，请联系上级请求补货！");
            view.addStaticAttribute("icon", 2);
        } else {
            int s = svc.check(housekeepOrder, number);
            if (s > 0) {
                view.addStaticAttribute("msg", "操作成功！");
                view.addStaticAttribute("icon", 1);
            } else {
                view.addStaticAttribute("msg", "操作失败！");
                view.addStaticAttribute("icon", 2);
            }
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "作废-房务订单")
    @RequestMapping("/checkOut")
    public ModelAndView checkOut(HousekeepOrder housekeepOrder, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        housekeepOrder.setEnployee_id_ratify(user.getId());
        housekeepOrder.setWorkdate_end(new Date());
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        int s = svc.checkOut(housekeepOrder);
        if (s > 0) {
            view.addStaticAttribute("msg", "操作成功！");
            view.addStaticAttribute("icon", 1);
        } else {
            view.addStaticAttribute("msg", "操作失败！");
            view.addStaticAttribute("icon", 2);
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "修改-房务订单")
    @RequestMapping("/update")
    public ModelAndView update(HousekeepOrder housekeepOrder) {
        return bc.update(svc, housekeepOrder);
    }

    @LogAnno(operateType = "删除-房务订单")
    @RequestMapping("/delete")
    public ModelAndView delete(HousekeepOrder housekeepOrder) {
        return bc.delete(svc, housekeepOrder);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(HousekeepOrder housekeepOrder) {
        return bc.findOne(svc, housekeepOrder);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, HousekeepOrder housekeepOrder, String dateRange) {
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }
        return bc.findPage(svc, page, housekeepOrder, housekeepOrder, begin, end);
    }

    @RequestMapping("/findDetails")
    public ModelAndView findDetails(String id) {
        // System.out.println(id);
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        HousekeepOrder order = new HousekeepOrder();
        order.setId(Integer.valueOf(id));
        view.addStaticAttribute("roomInfo", rsvc.find());
        view.addStaticAttribute("order", svc.findOne(order));
        view.addStaticAttribute("data", svc.findDetails(id));
        return new ModelAndView(view);
    }

}
